package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.chaosbean.auth.vo.DashBoardDataVO;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.adminservice.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 楚夏三分
 * @datetime 2023/1/2 9:16
 * @description: TODO:
 */
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    MarketAdminMapper adminMapper;
    @Autowired
    MarketRoleMapper roleMapper;
    @Autowired
    MarketPermissionMapper permissionMapper;
    @Autowired
    MarketAPIMapper apiMapper;
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    MarketOrderMapper orderMapper;

    @Override
    public AdminInfoBean getAdminInfo(String username) {
        AdminInfoBean infoBean = new AdminInfoBean();
        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username).andDeletedEqualTo(false);
        List<MarketAdmin> admins = adminMapper.selectByExample(adminExample);
        MarketAdmin admin = admins.stream().findFirst().orElse(new MarketAdmin());
        infoBean.setNickName(admin.getUsername());
        String avatar = admin.getAvatar();
        if (avatar == null) {
            infoBean.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        }
        infoBean.setAvatar(avatar);
        return infoBean;
    }

    @Override
    public List<String> getAdminRoles(String username) {
        Integer[] roleIds = getRoleIds(username);

        MarketRoleExample roleExample = new MarketRoleExample();
        MarketRoleExample.Criteria roleCriteria = roleExample.createCriteria();
        roleCriteria.andIdIn(Arrays.asList(roleIds))
                .andEnabledEqualTo(true).andDeletedEqualTo(false);
        List<MarketRole> marketRoles = roleMapper.selectByExample(roleExample);
        return marketRoles.stream().map(MarketRole::getName).collect(Collectors.toList());
    }

    @Override
    public List<String> getAdminPerms(String username) {
        Integer[] roleIds = getRoleIds(username);
        MarketPermissionExample2 example = new MarketPermissionExample2();
        MarketPermissionExample2.Criteria criteria = example.createCriteria();
        criteria.andRoleIdIn(Arrays.asList(roleIds)).andDeletedEqualTo(false);
        example.setDistinct(true);
        List<String> permissions = permissionMapper.selectByExample2(example).stream()
                .map(MarketPermission2::getPermission)
                .collect(Collectors.toList());
        if (permissions.contains("*")) {
            ArrayList<String> perms = new ArrayList<>();
            perms.add("*");
            return perms;
        }
        List<String> apis = apiMapper.selectApiOfPermissions(permissions);
        return apis;
    }



    @Override
    public DashBoardDataVO getDashBoardData() {
        MarketUserExample userExample = new MarketUserExample();
        userExample.createCriteria().andDeletedEqualTo(false);
        long userCount = userMapper.countByExample(userExample);

        MarketGoodsExample goodsExample = new MarketGoodsExample();
        goodsExample.createCriteria().andDeletedEqualTo(false);
        long goodsCount = goodsMapper.countByExample(goodsExample);

        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        productExample.createCriteria().andDeletedEqualTo(false);
        long prodCount = productMapper.countByExample(productExample);

        MarketOrderExample orderExample = new MarketOrderExample();
        orderExample.createCriteria().andDeletedEqualTo(false);
        long orderCount = orderMapper.countByExample(orderExample);

        return new DashBoardDataVO(goodsCount, userCount, prodCount, orderCount);
    }

    private Integer[] getRoleIds(String username) {
        // 此段代码冗余, 但懒得提取方法了, 蚌埠住了
        // 查询market_admin表中此admin对应的roles
        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        // 我突然发现criteria这玩意跟stream流一样返回自身对象,可以链式调用 <----> 优雅, 永不过时
        criteria.andUsernameEqualTo(username).andDeletedEqualTo(false);
        List<MarketAdmin> marketAdminsList = adminMapper.selectByExample(adminExample);

        // Stream流获取marketAdminsList中的roleIds
        // stream表示开启流式操作
        // findFirst表示取出list中第一个对象
        // orElse表示流中对象not null则返回对象本身, null则返回orElse函数中的对象
        Integer[] roleIds = marketAdminsList.stream()
                .findFirst().orElse(new MarketAdmin())
                .getRoleIds();
        return roleIds;
    }
}
